package com.beadwallet.alarm.web;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.beadwallet.alarm.service.msg.MsgInfoService;
import com.beadwallet.alarm.service.msg.MsgsendInfoService;
import com.beadwallet.alarm.service.msg.TitleService;
import com.beadwallet.alarm.entity.msgInfo.MsgInfo;
import com.beadwallet.alarm.entity.msgInfo.Title;
import com.beadwallet.alarm.service.alarm.AlarmInfoService;
import com.beadwallet.common.aspect.WebLog;
import com.beadwallet.common.controller.BaseController;
import com.beadwallet.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author master
 * @since 2018-04-02
 */
@RestController
@RequestMapping("msgInfo")
@Api(value = "msgInfo", description = "短信信息", tags = "msgInfo")
public class MsgInfoController extends BaseController {
    @Autowired
    private MsgInfoService msgInfoService;

    @Autowired
    private AlarmInfoService iAlarmInfoService;

    @Autowired
    private MsgsendInfoService msgsendInfoService;
    @Autowired
    private TitleService titleService;

    private static Logger log = LoggerFactory.getLogger(MsgInfoController.class);

    @WebLog(description = "查询短信内容及创建时间")
    @PostMapping("/queryContentAndTime")
    @ResponseBody
    public String queryContentAndTime(String jsonData){
        log.warn("queryContentAndTime jsonData:{}",jsonData);
        JSONObject object = JSONObject.parseObject(jsonData);
        String institution = object.getString("institution");
        String channel = object.getString("channel");
        String mobile = object.getString("mobile");
        List<Title> titles = titleService.selectTitle(mobile);
        //判断接口是否被调用
        /*if(msgsendInfos.size() > 0){
            boolean updateById = true;
            for (MsgsendInfo msgsendInfo : msgsendInfos) {
                msgsendInfo.setSynchronization(1);
                updateById = iMsgsendInfoService.updateById(msgsendInfo);
            }
            if (updateById){
                return respJson(CodeEnum.SUCC,msgsendInfos);
            }else{
                return respJson(CodeEnum.FAILURE,"您的信息有误");
            }
        }*/
        if(titles.size()<=0){
            return respJson(CodeEnum.RESPONSE_CODE_EMPTY);
        }else {
            return respJson(CodeEnum.SUCC, titles);
        }
    }

    @WebLog(description = "queryInfo")
    @PostMapping("/queryInfo")
    public List<MsgInfo> queryInfo(String templateId){
        Wrapper<MsgInfo> msgInfoEntityWrapper = new EntityWrapper<>();
        msgInfoEntityWrapper.eq("template_id",templateId);
        List<MsgInfo> msgInfos = msgInfoService.selectList(msgInfoEntityWrapper);
        return msgInfos;
    }
}
